package com.wgworkshop.pems.controller;

import com.wgworkshop.pems.checkgroup.CreateCheckGroup;
import com.wgworkshop.pems.checkgroup.PatchCheckGroup;
import com.wgworkshop.pems.checkgroup.UpdateCheckGroup;
import com.wgworkshop.pems.common.ResponseData;
import com.wgworkshop.pems.model.ClassHourModel;
import com.wgworkshop.pems.model.ClassHourViewRecord;
import com.wgworkshop.pems.service.ClassHourService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课时控制器。
 * Created by wlc on 2017/7/7.
 */
@RestController
@RequestMapping("/class-hours")
public class ClassHourController {
    @Autowired
    private ClassHourService classHourService;

    @PostMapping
    ResponseData<ClassHourModel> create(@RequestBody @Validated(CreateCheckGroup.class) ClassHourModel classHourModel) {
        ClassHourModel resultModel = classHourService.save(classHourModel);
        return new ResponseData<ClassHourModel>().success(resultModel);
    }

    @PostMapping("/all")
    ResponseData<List<ClassHourModel>> createAll(@RequestBody @Validated(CreateCheckGroup.class) List<ClassHourModel> classHourModels) {
        List<ClassHourModel> resultModels = classHourService.saveAll(classHourModels);
        return new ResponseData<List<ClassHourModel>>().success(resultModels);
    }

    @DeleteMapping("/{id}")
    ResponseData<Void> delete(@PathVariable("id") Long id) {
        classHourService.delete(id);
        return new ResponseData<Void>().success();
    }

    @PatchMapping("/{id}")
    ResponseData<ClassHourModel> update(@PathVariable("id") Long id,
                                        @RequestBody @Validated(PatchCheckGroup.class) ClassHourModel classHourModel) {
        ClassHourModel resultModel = classHourService.update(id, classHourModel);
        return new ResponseData<ClassHourModel>().success(resultModel);
    }

    @PutMapping
    ResponseData<ClassHourModel> update(@RequestBody @Validated(UpdateCheckGroup.class) ClassHourModel classHourModel) {
        ClassHourModel resultModel = classHourService.update(classHourModel);
        return new ResponseData<ClassHourModel>().success(resultModel);
    }

    @GetMapping("/{id}")
    ResponseData<ClassHourModel> findOne(@PathVariable("id") Long id) {
        ClassHourModel classHourModel = classHourService.findOne(id);
        return new ResponseData<ClassHourModel>().success(classHourModel);
    }

    @GetMapping("/search")
    ResponseData<List<ClassHourViewRecord>> search(@RequestParam(value = "name", required = false, defaultValue = "") String name) {
        List<ClassHourViewRecord> viewRecords = classHourService.getClassHourView(name);
        return new ResponseData<List<ClassHourViewRecord>>().success(viewRecords);
    }
}
